var form,$,table,layer,upload,element;
var data = [];
//查看个人微圈所需数据
var microCircleData =  JSON.parse(localStorage.getItem("microCircleData"));
$("input[name=microcircleId]").val(microCircleData.microcircleId);
data.push(microCircleData);
var user = JSON.parse(localStorage.getItem("user"));
var userId = user.userId;
layui.use(['form','jquery','layer','upload','element'],function() {
    form = layui.form;
    $ = layui.jquery;
    layer = layui.layer;
    table = layui.table;
    upload = layui.upload;
    element = layui.element;
    //展示点赞、收藏、评论、分享量
    showAnyNums(data);
    //展示个人微圈评论(4为微圈)
    showMicroCircleComment(microCircleData.microcircleId,4);
});
function showAnyNums(data) {
    table.render({
        elem: "#demo",
        data: data,
        width: 500,
        cols: [[
            {field: "acticleHits", title: "查看量"},
            {field: "acticleLike", title: "点赞量"},
            {field: "acticleShare", title: "分享量"},
            {field: "collectionNum", title: "收藏量", templet: function () {
                    var collectionNum = localStorage.getItem("collectionNum");
                    return collectionNum;
                }},
        ]]
    });
}

function showMicroCircleComment(microcircleId,type) {
    console.log(microcircleId);
    $.ajax({
        url: "/microcircle/getMicroCircleComment",
        type: "post",
        dataType: "json",
        data: {"commodityId": microcircleId,"type": type},
        success: function (data) {
            console.log(data.data);
            var html = "";
            var data = data.data;
            //对评论根据id进行排序
            data.sort(function (a,b) {
                return  b.id - a.id;
            });
            if (data != ''){
                for (var i = 0; i < data.length; i++){
                    if (data[i].userType == "1"){
                        data[i].userName = data[i].nickName;
                        data[i].imgUrl = data[i].avatarUrl;
                    }
                    if (data[i].userType == "2"){
                        data[i].userName = data[i].trueName;
                        data[i].imgUrl = data[i].acatarUrl;
                    }
                    if (data[i].display == "2") {
                        data[i].yinStatu = "隐藏";
                    }
                    if (data[i].display == "3") {
                        data[i].yinStatu = "显示";
                    }
                    if (data[i].status == "3") {
                        data[i].delStatu = "已删除";
                    }
                    if (data[i].status == "1") {
                        data[i].delStatu = "删除";
                    }
                }
                for(var a = 0; a < data.length; a++){
                    if (data[a].status == "1") {
                        html+=
                            "<div class='contentdiv'>" +
                            "<div class='contentdiv1'>" +
                            "<img id='imgId2' src='"+ data[a].imgUrl +"'>" +
                            "</div>" +
                            "<div class='singlediv'>" +
                            "<p class='nameclass'>"+ data[a].userName +"</p>" +
                            "<p class='contentclass'>回复网友:<span>"+ data[a].content +"</span></p>" +
                            "<input name='userId' style='display: none;' value='" + data[a].userId + "'>" +
                            "<p class='timeclass'>"+ data[a].commentDate+"</p>" +
                            "<div class='btnTwo'>" +
                            "<a class='a_class' href='javascript:;hideComment(\""+ data[a].id +"\",\""+ data[a].display+"\")'>"+ data[a].yinStatu +"</a>&nbsp" +
                            "<a class='a_class2' href='javascript:delectComment(\""+ data[a].id +"\")')'>"+ data[a].delStatu +"</a>" +
                            "</div>" +
                            "</div>" +
                            "</div>";
                    }else if (data[a].status == "3") {
                        html+=
                            "<div class='contentdiv'>" +
                            "<div class='contentdiv1'>" +
                            "<img id='imgId2' src='"+ data[a].imgUrl +"'>" +
                            "</div>" +
                            "<div class='singlediv'>" +
                            "<p class='nameclass'>"+ data[a].userName +"</p>" +
                            "<p class='contentclass'>回复网友:<span>"+ data[a].content +"</span></p>" +
                            "<input name='userId' style='display: none;' value='" + data[a].userId + "'>" +
                            "<p class='timeclass'>"+ data[a].commentDate+"</p>" +
                            "<div class='btnTwo'>" +
                            "<a class='a_class2' href='javascript:delectComment(\""+ data[a].id +"\")')'>"+ data[a].delStatu +"</a>" +
                            "</div>" +
                            "</div>" +
                            "</div>";
                    }
                }
                $("#comment_id").html(html);
            }else{
                $("#div_a").css("display","none");
            }
            $(".contentdiv .singlediv input").each(function () {
                if ($(this).val() == userId){
                    $(this).siblings(".contentclass").children("span").css({"color":"#44AFFF","font-weight":"600","font-size":"16px"});
                }
            });
        }
    });
}

//回复评论——确定提交
function sub() {
    var commodityId = $("input[name=microcircleId]").val();
    var content = $("#text_div1").val();
    //无内容时，点击确定给予提示
    if (content == ''){
        layer.msg("请输入内容");
        return true;
    }
    var data = {
        userId: userId,
        commodityId: commodityId,
        content: content,
        createId: userId,
        updateId: userId,
    }
    $.ajax({
        url: "/microcircle/addMicroCircleComment",
        type: "post",
        dataType: "json",
        data: data,
        success: function (data) {
            if (data.data){
                showMicroCircleComment(microCircleData.microcircleId,4);
                layer.msg("评论成功");
            }else{
                layer.msg("评论失败");
            }
        }
    });

}

//隐藏和显示评论
function hideComment(id,display) {
    if (display == "2"){
        display = "3";//变为隐藏
    } else if (display == "3"){
        display = "2";//变为显示
    }
    $.ajax({
        url: "/microcircle/hideComment",
        type: "post",
        dataType: "json",
        data: {
            "id": id,
            "display": display
        },
        success: function (data) {
            if (data.data){
                showMicroCircleComment(microCircleData.microcircleId,4);
            }
        }

    });
}

//删除评论
function delectComment(id) {
    $.ajax({
        url: "/microcircle/delComment",
        type: "post",
        dataType: "json",
        data: {"id": id,"status": 3},
        success: function (data) {
            if (data.data){
                showMicroCircleComment(microCircleData.microcircleId,4);
            }
        }
    });
}

//刷新
function refresh() {
    location.reload();
}